package com.cms.controller;

import com.cms.entity.User;
import com.cms.model.Result;
import com.cms.model.ResultCode;
import com.cms.service.UserService;
import com.cms.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping(path = "/user")
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping ("/")
    public String index1() {
        System.out.println("index1...");
        return "index";
    }

    @ResponseBody
    @RequestMapping (path = "/login",method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public Result userLogin(@RequestBody HashMap<String,String> userMap) {
        System.out.println(userMap);
        User res = userService.login(userMap);
        System.out.println(res);
        if (res != null) {
            return ResultUtil.success("登陆成功!");
        } else {
            return ResultUtil.error("用户名或密码错误");
        }
    }

    @ResponseBody
    @RequestMapping (path = "/find",method = RequestMethod.POST)
    public Result userFind() {
        List<User> users = userService.findAll();
        System.out.println(users);
        return ResultUtil.success(users);
    }



}
